<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/richText/bootstrap-combined.no-icons.min.css" type="text/css">
    <link rel="stylesheet" href="/css/bootstrap-responsive.min.css" type="text/css">
    <link href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="/richText/index.css" type="text/css"></link>

    <script src="/js/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="/richText/bootstrap-wysiwyg.js" type="text/javascript"></script>
    <script src="/richText/jquery.hotkeys.js" type="text/javascript"></script>
    <style>
        body, html {
            /*width: 100%;
            height: 100%;*/
            background: #fff;
            overflow: hidden;
        }

        #editor {
            height: 200px;
        }

        #main-content {
            margin-left: 5%;
        }

        #page-content {
            margin: 0;
            background: #FFF;
            /*padding: 8px 20px 24px;*/
            position: relative;
            top:0%;
            left:0%;
        }

        .modal-footer {
            background: #fff;
            position: relative;
            right: 10%;
        }
        .modal-body{
            position: relative;
            top:0%;
            left:20%;
        }
    </style>
</head>

<body>

<div id="main-content" class="clearfix">
    <div id="page-content" class="clearfix">


        <div class="page-header position-relative">
            <h1 style="color: #2679b5;font-size: 24px;font-weight: normal">品牌商
                <small style="font-size: 14px"><i class="icon-double-angle-right"></i> 公司信息</small>
            </h1>
        </div>
        <div class="modal-dialog" role="document">
            <div class="modal-content">

                <div class="modal-body" style="max-height:none">
                    <form class="form-horizontal">

                        <div class="control-group">
                            <label class="control-label">Company Name<br>公司名称(中)</label>
                            <div class="controls">
                                <input type="text" id="chineseName">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Company Name<br>公司名称(英)</label>
                            <div class="controls">
                                <input type="text" id="englishName">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Brief Introduction<br>公司简介</label>
                            <div class="controls">
                                <div>
                                    <textarea style="width: 250px" id="briefIntroduction"></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">GMC Report Type<br> 品牌商认证类型</label>
                            <div class="controls">
                                <input type="text" id="reportType">
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">GMC Report Url<br>证书地址</label>
                            <div class="controls">
                                <input type="text" id="repportUrl">
                            </div>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="btnupdate" onclick="update()">修改</button>
                    <button type="button" class="btn btn-primary" id="btnsave" onclick="inse()">添加</button>
                </div>
            </div>
        </div>

        <!-- Modal -->
        <div style="margin-left: -385px;width: 770px;" class="modal fade" id="myModal" tabindex="-1" role="dialog"
             aria-labelledby="myModalLabel">
        </div>
    </div>
</div>

<script>
    var companyID = ""
    var mvo_id = "";

    $(function () {
        var url = location.search;
        var Request = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1)　//去掉?号
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                Request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }

        mvo_id = Request["uid"]
        //将需要修改信息的商品ID传到该页面
        // $("#index").attr('href','/role?uid='+mvo_id);
    })


    //加載公司訊息
    $(function () {
        document.getElementById("btnsave").setAttribute("disabled", true)
        $.ajax({
            type: "post",
            url: "/user/selectCompanyByUid",
            dataType: "json",
            async: false,
            data: {id: mvo_id},
            success: function (company) {
                console.log(company);
                if(JSON.stringify(company)=="false"){
                    alert("请录入公司讯息1");
                    document.getElementById("btnsave").removeAttribute("disabled");
                    document.getElementById("btnupdate").setAttribute("disabled", true)
                }else{
                    //console.log(company)
                    companyID = company.id
                    $("#chineseName").val(company.chineseName)
                    $("#englishName").val(company.englishName)
                    $("#briefIntroduction").val(company.briefIntroduction)
                    $("#reportType").val(company.reportType)
                    $("#repportUrl").val(company.repportUrl)
                    // document.getElementById("btnsave").removeAttribute("disabled");
                    document.getElementById("btnupdate").removeAttribute("disabled");
                }
            },
            error: function () {
                alert("请录入公司讯息")
                document.getElementById("btnsave").removeAttribute("disabled");
                document.getElementById("btnupdate").setAttribute("disabled", true)
            }
        })
    })

    function update() {
        var id = companyID
        var chineseName = $("#chineseName").val()
        var englishName = $("#englishName").val()
        var briefIntroduction = $("#briefIntroduction").val()
        var reportType = $("#reportType").val()
        var repportUrl = $("#repportUrl").val()
        $.ajax({
            type: "post",
            url: "/company/updateCompany",
            dataType: "text",
            async: false,
            data: {
                id: id,
                Chinese_name: chineseName,
                English_name: englishName,
                brief_introduction: briefIntroduction,
                report_type: reportType,
                url: repportUrl
            },
            success: function (re) {
                alert(re)
            },
            error: function () {
                alert("更新公司信息出错")
            }

        })
        window.parent.location.reload()
    }


    function inse() {

        var chineseName = $("#chineseName").val()
        var englishName = $("#englishName").val()
        var briefIntroduction = $("#briefIntroduction").val()
        var reportType = $("#reportType").val()
        var repportUrl = $("#repportUrl").val()

        $.ajax({
            type: "post",
            url: "/company/addCompany",
            dataType: "text",
            async: false,
            data: {
                Chinese_name: chineseName,
                English_name: englishName,
                brief_introduction: briefIntroduction,
                report_type: reportType,
                url: repportUrl,
                mvoId: mvo_id
            },
            success: function (re) {
                //添加公司讯息成功后立刻生成对应的仓库
                $.ajax({
                    type: "post",
                    url: "/storehouse/addStorehouse",
                    dataType: "json",
                    async: false,
                    data: {id: mvo_id, name: mvo_id, movId: mvo_id},
                    success: function (re) {
                        console.log(re)
                    },
                    error: function () {
                        console.log("添加仓库出错")
                    }
                })
                alert(re)
            },
            error: function () {
                alert("erroraddCompany")
            }
        })
        window.location.reload()
    }
</script>
</body>
</html>
